<?php

defined('SYSPATH') or die('No direct script access.');

class User {

    private static $instances;
    private $data;

    public static function & instance($id = 0) {
        if (!isset(self::$instances[$id])) {
            $class = __CLASS__;
            self::$instances[$id] = new $class($id);
        }
        return self::$instances[$id];
    }

    public function __construct($id) {
        $this->_load($id);
    }

    public function _load($id) {
        if ($id) {
            $user = ORM::factory('cheers_sec_user', $id);
            if ($user->loaded()) {
                $data = $user->as_array();
                $data['chr_password'] = '';
            }
            $this->data = $data;
        }
    }

    public function get($key = NULL) {
        if (empty($key)) {
            return $this->data;
        } else {
            return isset($this->data[$key]) ? $this->data[$key] : '';
        }
    }

    public function login($data) {
        $email = $data['username'];
        $password = $data['password'];
        $user = ORM::factory('cheers_sec_user')
            ->where('chr_loginname', '=', $email)
            ->where('chr_password', '=', toolkit::hash($password))
            ->find();
			
        $user->loaded() ? $user_id = $user->chr_uid : $user_id = 0;
        return $user_id;
    }

    public function logged_in() {
        $session = Session::instance();
        $user_id = $session->get('chr_uid');
        if (isset($user_id) AND $user_id != 0) {
            $user = User::instance($user_id)->get();
            return $user;
        } else {
            return FALSE;
        }
    }

    public function register($data) {
        $user = ORM::factory('cheers_sec_user');
        // hash password
        $data['chr_password'] = toolkit::hash($data['chr_password']);

        $user->values($data);
        if ($user->check()) {
            $user->chr_createdate = time();
            $user->chr_creator = $this->logged_in();
            $user->chr_editdate = time();
            $user->chr_lastlogindate = time();
            $user->chr_logintimes = 1;
            $user->save();
            return $user->chr_uid;
        } else {
            return FALSE;
        }
    }

    public function is_register($chr_loginname) {
        $user = ORM::factory('cheers_sec_user')
                ->where('chr_loginname', '=', $chr_loginname)
                ->find();

        $user->loaded() ? $user_id = $user->chr_uid : $user_id = 0;
        return $user_id;
    }

    public function task($user_id) {
        $orm = ORM::factory('usertask')->where('user_id', '=', $user_id)->find();
        if ($orm->loaded()) {
            return $orm->as_array();
        }
        else
            return false;
    }

    public function delete($id) {
        $orm = ORM::factory('cheers_sec_user', $id);

        if ($orm->loaded()) {
            return $orm->delete();
        }
        else
            return false;
    }

    public function edit($id, $data) {
        $role = ORM::factory('cheers_sec_user')
                ->where('chr_uid', '=', $id)
                ->find();
        $role->values($data);
        
        
    

        if ($role->check()) {
            $role->save();
            return true;
        }
        else
            return false;
    }

}
